package cn.xiaoming.car.server.content.dao.persist.repository.Impl;

import cn.xiaoming.car.pojo.car.pojo.entity.Car;
import cn.xiaoming.car.pojo.car.pojo.entity.CarDetail;
import cn.xiaoming.car.pojo.car.pojo.vo.CarListItemVO;
import cn.xiaoming.car.pojo.car.pojo.vo.CarVO;
import cn.xiaoming.car.server.content.dao.persist.mapper.CarDetailMapper;
import cn.xiaoming.car.server.content.dao.persist.mapper.CarMapper;
import cn.xiaoming.car.server.content.dao.persist.repository.ICarRepository;
import cn.xiaoming.car.server.pojo.vo.PageData;
import cn.xiaoming.car.server.util.PageInfoToPageDataConverter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Component
public class CarRepositoryImpl implements ICarRepository {

    @Autowired
    CarMapper carMapper;
    @Autowired
    CarDetailMapper carDetailMapper;

    private CarRepositoryImpl(){
        log.info("创建储存数据库对象：CarRepositoryImpl");
    }


    @Override
    public int insert(Car car) {
        log.debug("开始执行【向车辆表中插入数据】，参数：{}", car);
        return carMapper.insert(car);
    }

    @Override
    public int deleteById(Long id) {
        log.debug("开始执行【根据ID删除车辆数据】，参数：{}", id);
        return carMapper.deleteById(id);
    }

    @Override
    public int deleteDetailById(Long carId) {
        log.debug("开始执行【根据ID删除车辆数据】，参数：{}", carId);
        return carMapper.deleteById(carId);
    }

    @Override
    public int updateById(Car car) {
        log.debug("开始执行【根据ID修改车辆数据】，参数：{}", car);
        return carMapper.updateById(car);
    }


    @Override
    public int countByNum(String plateNum) {
        log.debug("开始执行【根据名称\"{}\"统计车辆表中车牌的数量】，参数：", plateNum);
        return  carMapper.selectCount(Wrappers.<Car>lambdaQuery().eq(Car::getPlateNumber,plateNum));

    }

    @Override
    public int countByNumAndId(Long id, String plateNum) {
        log.debug("开始执行【统计匹配车牌号查不匹配ID的车牌号数量】，ID：{}，名称：{}", id, plateNum);
        QueryWrapper<CarDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("plate_number", plateNum).ne("id", id);
        return carDetailMapper.selectCount(queryWrapper);
    }

    @Override
    public CarListItemVO getCarById(Long id) {
        log.debug("开始执行【根据ID查询车辆】，参数:{}", id);
        return carMapper.getCarById(id);
    }

    @Override
    public CarVO getDetailById(Long id) {
        log.debug("开始执行【根据ID查询车辆】，参数:{}", id);
        return carMapper.getDetailById(id);
    }

    @Override
    public PageData<CarListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【根据ID查询车辆列表】，页码{}，每页记录数：{}", pageNum,pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<CarListItemVO> list = carMapper.list();
        PageInfo<CarListItemVO> pageInfo = new PageInfo<>(list);
        PageData<CarListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }


}
